<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/ZendSkeletonModule for the canonical source repository
 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace TestModule\Controller;

use Zend\View\Helper\ViewModel;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\Session\Container as SessionContainer;
use TestModule\Form\LoginForm;

class IndexController extends AbstractActionController
{
    
	protected $sessionContainer;
	
	private function getSessionContainer() 
	{
		if(!$this->sessionContainer)
		{
			$this->sessionContainer = new SessionContainer('user');
		}
		
		return $this->sessionContainer;
	}
	
	
	public function testHelperAction()
	{
		return array();
	}
	
	public function indexAction()
    {
    	if(!$this->getSessionContainer()->helloString)
    	{
    		return $this->forward()->dispatch('TestModule\Controller\Index', array('action' => 'login'));
    	}
    	else
    	{
    		return $this->redirect()->toRoute('test-module/go', array('controller' => 'index', 'action' => 'greeting'));
    	}
    	return array();
    }

    public function loginAction()
    {
		if($this->getSessionContainer()->helloString)
		{
			return $this->redirect()->toRoute('test-module/go', array('controller' => 'index', 'action' => 'greeting'));
		}
		
    	$request = $this->getRequest();
    		
    	$login_form = new LoginForm(); 
    		
    	if($request->isPost())
    	{
    		$name = $request->getPost('username');
    			
    		if($name) 
    		{
    			$this->getSessionContainer()->helloString = "Hello " . $name .',';
    				
    			return $this->redirect()->toRoute('test-module/go', array('controller' => 'index', 'action' => 'greeting'));
    		}
    	}

       	return array('login_form' => $login_form);
    }
    
    public function logoutAction() 
    {
    	$this->getSessionContainer()->offsetUnset('helloString');
    	
    	return $this->redirect()->toRoute('test-module');

    }
    
    public function greetingAction()
    {
    	if(!$this->getSessionContainer()->helloString) {
    		return $this->forward()->dispatch('TestModule\Controller\Index', array('action' => 'login'));
    		
    	}
		
    	return array('helloString' => $this->getSessionContainer()->helloString);
    }
}
